const activityDao = require('../dao/activityDao');
const fileDao = require('../dao/fileDao');

const activityService = {
    async getActivityList(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await activityDao.getActivityList(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                let tmp = await fileDao.getActivityImgUrl(result[i].imgId);
                if (tmp.success) {
                    result[i].imageUrl = tmp.url;
                } else {
                    result[i].imageUrl = '/static/activity_default.jpg';
                }
            }
            result = { code: 200, msg: '获取活动列表成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async joinActivity(uid, activityId) {
        let result;
        try {
            result = await activityDao.joinActivity(uid, activityId);
            if (result.insertId) {
                result = { code: 200, msg: '参加活动成功', data: null };
            } else {
                result = { code: 500, msg: '请勿重复参加活动', data: null };
            }
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async cancelActivity(uid, activityId) {
        let result;
        try {
            result = await activityDao.cancelActivity(uid, activityId);
            if (result.affectedRows) {
                result = { code: 200, msg: '取消活动成功', data: null };
            } else {
                result = { code: 500, msg: '你并未参加此活动', data: null };
            }
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getActivityInfo(uid, activityId) {
        let result;
        try {
            result = await activityDao.getActivityInfo(uid, activityId);
            if (result.length) {
                result = result[0];
                let tmp = await fileDao.getActivityImgUrl(result.imgId);
                if (tmp.success) {
                    result.imgUrl = tmp.url;
                } else {
                    result.imgUrl = '';
                }
                result = { code: 200, msg: '获取活动详情成功', data: result };
            } else {
                result = { code: 500, msg: '未找到该活动', data: null };
            }
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getJoinedActivityList(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await activityDao.getJoinedActivityList(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                let tmp = await fileDao.getActivityImgUrl(result[i].imgId);
                if (tmp.success) {
                    result[i].imageUrl = tmp.url;
                } else {
                    result[i].imageUrl = '/static/activity_default.jpg';
                }
            }
            result = { code: 200, msg: '获取已参加活动列表成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    }
};

module.exports = activityService;